package com.sun.draw.command;

import java.text.DecimalFormat;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.draw.api.AbstractDrawCommand;
import com.sun.draw.api.IDrawCommand;
import com.sun.draw.param.DrawParamContent;
import com.sun.draw.pojo.InstantDrawInfo;
import com.sun.util.DrawUtils;
/**
 * 随机中奖概率计算
* @ClassName: RandomWinCommand  
* @author sunt  
* @date 2018年1月11日 
* @version V1.0
 */
public class RandomWinCommand extends AbstractDrawCommand implements IDrawCommand {

	/**
	 * 日志
	 */
	private Logger logger = LoggerFactory.getLogger(RandomWinCommand.class);
	
	@SuppressWarnings("unused")
	public boolean execute(DrawParamContent drawParamContent) {
		//1、获取奖品列表(QueryInstantCommand从数据库查询到将奖品信息添加到DrawParamContent中)
		List<InstantDrawInfo> list = drawParamContent.getInstantDrawInfoList();
		//2、生成随机数与奖品概率范围对比
		InstantDrawInfo instantDrawInfo = null;
		if(null != list && list.size() > 0) {
			double randomNumber = Math.random();//生成随机数
			DecimalFormat dFormat = new DecimalFormat("#.####");
			//logger.info("随机中奖概率范围计算...");
			List<Double> listProbability = DrawUtils.getProbabilityRange(list);
			
			// 中奖策略的位置
			int listDex = -1;
			for (int i = 0; i < listProbability.size(); i++) {
				if (randomNumber <= listProbability.get(i)) {
					// 如果随机数在 （i-1）——i 之间 则中奖策略位置为（i-1）
					listDex = (i - 1);
					break;
				} else {
					listDex = i;
				}
			}
			
			if (listDex >= 0 && listDex < list.size()) {
				instantDrawInfo = list.get(listDex);
			}
			
			//3、获取中奖位置的奖品
			if(null != instantDrawInfo) {
				//扣减库存
				// 设置随机数获得的抽奖策略
				drawParamContent.setInstantDrawInfo(instantDrawInfo);
				// 处理抽奖状态信息
				drawParamContent.getWinDrawInfo().setWinFlag("true");
				logger.info("---->抽中的奖品名称:" + instantDrawInfo.getPrizeName() + ",中奖提示语:" + instantDrawInfo.getPrizeTips());
			}
		}
		
		return true;
	}

}
